import {Command} from '../constant';
import Taro from '@tarojs/taro';
import {Dispatch} from 'typings';
import {IAllReducerProps} from '../types';
import {getReducerData} from '@/redux/store';
import {extraPathsValue} from '@/redux/util';
import api from 'api';

export default (dispatch: Dispatch) => {
  let action = {
    commonChange(...param: any) {
      dispatch({
        type: Command.commonChange,
        payload: extraPathsValue(...arguments),
      });
    },
    //获取我的奖励
    async getMyReward() {
      let {
        main: {pageNum, yearMonth, myRewardList},
      } = getData();
      console.log('pageNum===', getData().main.pageNum);
      try {
        Taro.showLoading();
        let params = {
          yearMonth,
          pageNum,
          pageSize: 10,
        };
        const res = await api.PrizesController.getMyRewardApi(params);
        console.log('获取我的奖励==res===', res);

        if (res.code == 'K-000000') {
          if (pageNum == 0) {
            action.commonChange('main.myRewardList', res.context.content);
          } else {
            let newList = myRewardList.concat(res.context.content);
            console.log('newList===', newList);
            action.commonChange('main.myRewardList', newList);
          }
          action.commonChange('main.totalPages', res.context.totalPages);
          action.commonChange('main.pageNum', res.context.number);
        }
      } catch (e) {
        console.log('获取我的奖励==e===', e);
      } finally {
        Taro.hideLoading();
      }
    },
    async toOrderDetali(goodsInfoId, item) {
      const toBay = await api.MyGiftPackController.freeBuy({
        goodsInfoIds: [goodsInfoId],
        // isGiftpacks: true,
        myRewardId: item.myRewardId,
        whetherLottery: true,
      });
      Taro.navigateTo({
        url: '/pages/package-C/order/order-confirm-gift/index',
      });
    },
  };
  return action;
};

function getData(): IAllReducerProps {
  return {
    main: getReducerData('packageCPrizeRecordtMain'),
  };
}

//create by moon https://github.com/creasy2010/moon
